package com.itheima.controllor;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
import service.CheckItemsGroupService;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckItemsGroupControllor {

    @Reference
    private CheckItemsGroupService checkItemsGroupService;

    @Autowired
    private JedisPool jedisPool;
    /**
     * 新增检查项目
     * @param
     * @return
     */
    @RequestMapping("/addItemsGroup")
    public Result addItemsGroup( @RequestBody CheckGroup CheckGroup, int[] checkitemIds){
        try {
            checkItemsGroupService.add(CheckGroup,checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }
    /**
     * 一进检查组页面,搜索出全部,并且分页.还可以根据查询
     * 前端是传递 当前页码
     *            每页的记录数
     *            查询条件
     */
    @RequestMapping("/findAll")
    public Result findAll( @RequestBody QueryPageBean QueryPageBean){
        try {
            PageResult pageResult=checkItemsGroupService.findAll(QueryPageBean,QueryPageBean);
            /**
             *  返回有: 总条数
                        当前页结果 list的集合
             */
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL,null);

        }
    }


    /**
     * 根据检查组id查询检查组对象
     */
    @RequestMapping("/findcheckGroupById")
    public Result findcheckGroupById( int id){
        try {
            CheckGroup checkGroup=checkItemsGroupService.findcheckGroupById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL,null);

        }
    }

    /**
     * 根据检查组id查询检查项ids
     */
    @RequestMapping("/findCheckitemIds_BycheckgroupId")
    public List<Integer> findCheckitemIds_BycheckgroupId(int id){

        try {
            List<Integer> itemList=checkItemsGroupService.findCheckitemIds_BycheckgroupId(id);
            return itemList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 修改检查组数据
     */
    @RequestMapping("/updateItemsGroup")
    public Result updateItemsGroup(@RequestBody CheckGroup checkGroup,int[] checkitemIds ){
        try {
            checkItemsGroupService.updateItemsGroup(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);

        }
    }


    /**
     * 查找所有的检查组,用于添加套餐的弹出窗口
     */
    @RequestMapping("/findAll2")
    public Result findAll2(){
        try {
             List<CheckGroup> checkGroupList=checkItemsGroupService.findAll2();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL,null);

        }
    }





}
